package org.budo.support.jedis.util;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.JedisPubSub;

/**
 * @author lmw
 */
@Slf4j
public class BudoJedisPubSub extends JedisPubSub {
    private Object jedisSubscriber;

    public BudoJedisPubSub(Object JedisSubscriber) {
        this.jedisSubscriber = JedisSubscriber;
    }

    @Override
    public void onMessage(String channel, String message) {
        if (null != this.jedisSubscriber && this.jedisSubscriber instanceof OnMessage) {
            ((OnMessage) this.jedisSubscriber).onMessage(channel, message);
            return;
        }

        log.info("#13 onMessage, channel=" + channel + ", message=" + message);
    }

    @Override
    public void onPMessage(String pattern, String channel, String message) {
        if (null != this.jedisSubscriber && this.jedisSubscriber instanceof OnPMessage) {
            ((OnPMessage) this.jedisSubscriber).onPMessage(channel, message);
            return;
        }

        log.info("#18 onPMessage, pattern=" + pattern + ", channel=" + channel + ", message=" + message);
    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        if (null != this.jedisSubscriber && this.jedisSubscriber instanceof OnSubscribe) {
            ((OnSubscribe) this.jedisSubscriber).onSubscribe(channel, subscribedChannels);
            return;
        }

        log.info("#23 onSubscribe, channel=" + channel + ", subscribedChannels=" + subscribedChannels);
    }

    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        if (null != this.jedisSubscriber && this.jedisSubscriber instanceof OnUnsubscribe) {
            ((OnUnsubscribe) this.jedisSubscriber).onUnsubscribe(channel, subscribedChannels);
            return;
        }

        log.info("#28 onUnsubscribe, channel=" + channel + ", subscribedChannels=" + subscribedChannels);
    }

    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
        if (null != this.jedisSubscriber && this.jedisSubscriber instanceof OnPUnsubscribe) {
            ((OnPUnsubscribe) this.jedisSubscriber).onPUnsubscribe(pattern, subscribedChannels);
            return;
        }

        log.info("#33 onPUnsubscribe, pattern=" + pattern + ", subscribedChannels=" + subscribedChannels);
    }

    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {
        if (null != this.jedisSubscriber && this.jedisSubscriber instanceof OnPSubscribe) {
            ((OnPSubscribe) this.jedisSubscriber).onPSubscribe(pattern, subscribedChannels);
            return;
        }

        log.info("#38 onPSubscribe, pattern=" + pattern + ", subscribedChannels=" + subscribedChannels);
    }
}